package com.kun.alipay.service.impl;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.kun.alipay.config.AlipayConfig;
import com.kun.alipay.exception.BusinessException;
import com.kun.alipay.exception.ErrorCode;
import com.kun.alipay.model.Payment;
import com.kun.alipay.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 支付宝支付服务实现类（基于EasySDK）
 */
@Slf4j
@Service
public class PaymentServiceImpl implements PaymentService {

    @Resource
    private AlipayConfig alipayConfig;

    @Override
    public String pay(Payment payment) {
        try {
            AlipayTradePagePayResponse response = Factory.Payment
                    .Page()
                    .pay(payment.getSubject(), payment.getOrderId(), payment.getAmount().toString(), alipayConfig.getReturn_url());
            return response.body;
        } catch (Exception e) {
            log.error("支付宝支付失败", e);
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "支付宝支付失败: " + e.getMessage());
        }
    }
}
